package com.sm.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sm.model.*;
import com.sm.service.*;
import com.sm.util.Msg;
import com.sm.vo.VodVO;
import com.sm.vod.AliyunUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程视频Controller
 */
@Controller
@RequestMapping("/video")
public class VideoCourseController {

    @Autowired
    private VideoCourseService videoCourseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private MyVipService myVipService;
    @Autowired
    private SecondDirectoryService secondDirectoryService;
    @Autowired
    private FirstDirectoryService firstDirectoryService;

    /**
     * 课程详情页
     */
    @RequestMapping("/selectOne")
    @ResponseBody
    public Msg selectOne(Integer userId, Integer videoId) throws Exception {
        if (userId == null && videoId == null) {
            return Msg.fail().add("msg", "参数错误");
        }
        VideoCourse videoCourse = videoCourseService.selectHomeByPrimaryKey(videoId);
        if (ObjectUtil.isEmpty(videoCourse)) {
            return Msg.fail().add("msg", "数据找不到啦！");
        }
        FirstDirectory firstDirectory = firstDirectoryService.selectOne(videoCourse.getFirstDirectoryId());
        Teacher teacher = teacherService.selectByPrimaryKey(videoCourse.getTeacherId());//通过老师id查询老师
        boolean lookFlag = true;
        List<VideoCourse> classMates = new ArrayList<>();
        if (firstDirectory.getIfSchool() == -1) {
            //说明是首页的课程，进行会员判断
            Integer topType = myVipService.getTopType(userId);
            if (topType == null || topType < 6) {
                if (videoCourse.getIsFree() == 0) {
                    lookFlag = false;
                }
            }
            //判断家庭教育需要家庭教育会员才可以访问
            if (firstDirectory.getFirstDirectoryId() == 1) {
                lookFlag = videoCourse.getIsFree() == 1 || (topType != null && (topType == 0 || topType >= 6));
            }

            if (ObjectUtil.isEmpty(teacher)) {
                classMates = null;
            } else {
                if (videoCourse.getFirstDirectoryId() != 4) {
                    classMates = videoCourseService.selectClassMates(videoCourse.getSecondDirectoryId(), videoCourse.getTeacherId());
                } else {
                    classMates = videoCourseService.selectTopSchoolClassMates(videoCourse.getSecondDirectoryId());
                }
            }
        } else {
            //是状元学堂的课程
            lookFlag = checkVipRole(userId, videoCourse.getFirstDirectoryId(), videoCourse);
            classMates = videoCourseService.selectTopSchoolClassMates(videoCourse.getSecondDirectoryId());
        }
        Collect collect = collectService.selectCollectByVideoId(userId, videoCourse.getVideoCourseId(), 1);//查询当前视频是否已经收藏
        int count = 1;//默认当前视频已经是收藏状态
        if (collect == null) {
            count = -1;//设置视频状态为没有收藏
        }
        SecondDirectory secondDirectory = secondDirectoryService.selectOne(videoCourse.getSecondDirectoryId());
        VideoCourse fixVideo = fixVideo(videoCourse);
        String teacherName = "";
        if (lookFlag) {
            updateLearnNum(videoId, 7);
            return Msg.success().add("count", count).add("videoCourse", fixVideo).add("categoryName", secondDirectory.getAlias()).add("teacherName", teacherName).add("classMates", classMates);
        } else {
            return Msg.pay();
        }

    }

    //修改课程的学习人数
    public void updateLearnNum(Integer videoId, Integer plus) {
        VideoCourse videoCourse = videoCourseService.selectByPrimaryKey(videoId);
        VideoCourse course = new VideoCourse();
        course.setVideoCourseId(videoId);
        course.setLearnNumber(videoCourse.getLearnNumber() + plus);
        videoCourseService.updateVideo(course);
    }

    /**
     * 判断一个用户能否观看状元学堂的此视频课程
     */
    public boolean checkVipRole(Integer userId,Integer firstDirectoryId,VideoCourse videoCourse){
        Integer topType = myVipService.getTopType(userId);
        //我的vipId列表
        List<Integer> myVipIdList = myVipService.getMyVipIdList(userId);

        if (videoCourse.getIsFree()==null || videoCourse.getIsFree().equals(1)) {
            return true;
        }
        if (topType == null){
            return false;
        }
        if (topType > 6) {
            return true;
        }
        switch (firstDirectoryId){
            case 5:
                if (myVipIdList.contains(3)||myVipIdList.contains(6)){
                    return true;
                }
                break;
            case 6:
                if (myVipIdList.contains(5)||myVipIdList.contains(6)){
                    return true;
                }
                break;
            case 7:
                if (myVipIdList.contains(4)||myVipIdList.contains(6)){
                    return true;
                }
                break;
            case 9:
            case 10:
            case 11:
                if (myVipIdList.contains(1)){
                    return true;
                }
                break;
            case 12:
                if (myVipIdList.contains(2)){
                    return true;
                }
                break;
            default:
                return true;
        }
        return false;
    }

    //视频详情装配上阿里播放三个清晰度的url
    public VideoCourse fixVideo(VideoCourse videoCourse) throws Exception {
        VodVO playInfo = AliyunUpload.getPlayInfo(videoCourse.getAliVideoId());
        videoCourse.setCoverUrl(playInfo.getCoverUrl());
        videoCourse.setHdUrl(playInfo.getHdUrl());
        videoCourse.setLdUrl(playInfo.getLdUrl());
        videoCourse.setSdUrl(playInfo.getSdUrl());
        return videoCourse;
    }



}
